package com.market.cash.ui.adapter

import android.view.View
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.market.cash.R
import com.market.cash.utils.CashRootImageUtil
import com.market.cash.utils.adapter.CashRootRecyclerViewAdapter
import com.market.cash.utils.adapter.CashRootViewHolderHelper
import org.json.JSONObject
import java.math.BigDecimal

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/18 10:55
 * @Annotation：首页产品列表
 */
class CashRootHomeAdapter(recyclerView: RecyclerView?) : CashRootRecyclerViewAdapter<JSONObject>(recyclerView) {

    private val CASHROOT_TYPE_GRADE = "TYPE_GRADE"//等级
    private val CASHROOT_TYPE_REPAY_COUNT = "TYPE_REPAY_COUNT"//待还款的产品数量
    private val CASHROOT_TYPE_PRODUCT = "TYPE_PRODUCT"//产品
    private val CASHROOT_TYPE_UNLOCK_TIPS = "TYPE_UNLOCK_TIPS"//解锁提示

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when(model.optString("itemType")){
            CASHROOT_TYPE_GRADE -> {//等级
                R.layout.cash_home_item_grade
            }
            CASHROOT_TYPE_REPAY_COUNT -> {//待还款数量
                R.layout.cash_home_item_repay_tips
            }
            CASHROOT_TYPE_UNLOCK_TIPS -> {//解锁提示
                R.layout.cash_home_item_unlock_tips
            }
            else -> {//产品
                R.layout.cash_home_item_product
            }
        }
    }

    override fun setItemChildListener(helper: CashRootViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.repayLayout)//待还款
        helper.setItemChildClickListener(R.id.btnApply)//申请
        helper.setItemChildClickListener(R.id.inReviewLayout)//审核中、放款中
        helper.setItemChildClickListener(R.id.rejectedLayout)//已拒绝
    }

    override fun fillData(helper: CashRootViewHolderHelper, position: Int, model: JSONObject) {
        when(model.optString("itemType")){
            CASHROOT_TYPE_GRADE -> {//等级
                showCashRootGradeView(helper, position, model)
            }
            CASHROOT_TYPE_REPAY_COUNT -> {//待还款数量
                showCashRootRepayCountView(helper, position, model)
            }
            CASHROOT_TYPE_UNLOCK_TIPS -> {
            }
            else -> {
                showCashRootProductView(helper, position, model)
            }
        }
    }

    /**
     * 等级View
     */
    private fun showCashRootGradeView(helper: CashRootViewHolderHelper, position: Int, model: JSONObject){
        val gradeLayout = helper.getView<RelativeLayout>(R.id.gradeLayout)
        val imgGradeIcon = helper.getImageView(R.id.imgGradeIcon)
        when(model.optInt("grade")){
            2 -> {
                gradeLayout.setBackgroundResource(R.mipmap.cash_grade_gold_bg)
                imgGradeIcon.setImageResource(R.mipmap.cash_grade_gold_icon)
                helper.setText(R.id.textGrade,mContext.getString(R.string.home_grade_gold))
            }
            3 -> {
                gradeLayout.setBackgroundResource(R.mipmap.cash_grade_platinum_bg)
                imgGradeIcon.setImageResource(R.mipmap.cash_grade_platinum_icon)
                helper.setText(R.id.textGrade,mContext.getString(R.string.home_grade_platinum))

            }
            else -> {
                gradeLayout.setBackgroundResource(R.mipmap.cash_grade_silver_bg)
                imgGradeIcon.setImageResource(R.mipmap.cash_grade_silver_icon)
                helper.setText(R.id.textGrade,mContext.getString(R.string.home_grade_silver))
            }
        }
    }

    /**
     * 待还款数量
     */
    private fun showCashRootRepayCountView(helper: CashRootViewHolderHelper, position: Int, model: JSONObject){
        helper.setText(R.id.textRepayCount,String.format(mContext.getString(R.string.home_repay_count),model.optString("count")))
    }

    /**
     * 产品
     */
    private fun showCashRootProductView(helper: CashRootViewHolderHelper, position: Int, model: JSONObject){
        val imgLogo = helper.getImageView(R.id.imgLogo)
        CashRootImageUtil.instance.loadImageUrl(mContext,imgLogo,model.optString("logo"),R.mipmap.cash_holder)
        helper.setText(R.id.textTitle,model.optString("name"))

        val textAmount = helper.getTextView(R.id.textAmount)
        val showAmount = BigDecimal(model.optString("amount")).stripTrailingZeros().toPlainString()
        textAmount.text = showAmount

        helper.setText(R.id.textDays,String.format(mContext.getString(R.string.home_product_days),model.optString("days")))

        helper.setVisibility(R.id.btnApply, View.GONE)
        helper.setVisibility(R.id.inReviewLayout,View.GONE)
        helper.setVisibility(R.id.rejectedLayout,View.GONE)
        helper.setVisibility(R.id.lockLayout,View.GONE)
        when(model.optString("activeFlag")){
            "YES" -> {//已解锁
                if (model.optString("applyStatus").endsWith("DENIED")){//被拒绝
                    helper.setVisibility(R.id.rejectedLayout,View.VISIBLE)
                } else {
                    val status = model.optString("status")
                    if (status == "APPLY"){
                        //可申请
                        helper.setVisibility(R.id.btnApply, View.VISIBLE)
                    } else {
                        helper.setVisibility(R.id.inReviewLayout,View.VISIBLE)
//                        if (model.optString("applyStatus").endsWith("DENIED")) {//被拒绝
//                            helper.setVisibility(R.id.rejectedLayout,View.VISIBLE)
//                        } else {//进行中
//                            helper.setVisibility(R.id.inReviewLayout,View.VISIBLE)
//                        }
                    }
                }
            }
            else -> {//未解锁
                helper.setVisibility(R.id.lockLayout,View.VISIBLE)
            }
        }
    }
}